//
//  MsgCtrl.m
//  Traveling
//
//  Created by Northwan on 15/5/12.
//  Copyright (c) 2015年 长沙市岳麓区立早网络科技有限公司. All rights reserved.
//  资讯列表

#import "MsgCtrl.h"
#import "MsgCell.h"
#import "MsgDetailCtrl.h"
#import "MsgDataTool.h"
#import "MessageResult.h"

@interface MsgCtrl ()<UITableViewDataSource,UITableViewDelegate,UISearchBarDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet UISearchBar *searchBar;
@property (strong, nonatomic) NSMutableArray *itemArray;
@property (copy,nonatomic) NSString *url;
@end

@implementation MsgCtrl

-(NSMutableArray *)itemArray{
    if (!_itemArray) {
        _itemArray = [NSMutableArray array];
    }
    return _itemArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.url = @"news/news_list.json";//旅游新闻
    [self.searchBar setDelegate:self];
    [self initWithTableView];
    [self.tableView headerBeginRefreshing];
}

-(void)initWithTableView{
    [self.tableView setDelegate:self];
    [self.tableView setDataSource:self];
    [self.tableView addHeaderWithTarget:self action:@selector(headerRefresh)];
    [self.tableView addFooterWithTarget:self action:@selector(footerRefresh)];
}

-(void)headerRefresh{
    [self requestDataWithMaxId:0 url:self.url];
}

-(void)footerRefresh{
    NSInteger maxId = 0;
    if(self.itemArray.count){
       MessageResult *result  = [self.itemArray lastObject];
       maxId =  result.id;
    }
    if(maxId > 0){
        maxId--;
    }

    [self requestDataWithMaxId:maxId url:self.url];
}

-(void)requestDataWithMaxId:(NSInteger)maxId url:(NSString *)url {
    [MsgDataTool getMsgList:maxId url:url keywords:self.searchBar.text success:^(MessageRes *result) {
        [Utils refreshComplete:self.tableView];
        if(result.isSucc){
            if(maxId == 0){
                [self.itemArray removeAllObjects];
            }
            [self.itemArray addObjectsFromArray:result.datas];
            [self.tableView reloadData];
        }else{
            [DialogUtils showTips:result.msg];
        }
    } failure:^(NSError *error) {
         [Utils refreshComplete:self.tableView];
         [DialogUtils showTips:@"加载失败"];
    }];
}

-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
    [self.tableView headerBeginRefreshing];
}

- (IBAction)titleItemTap:(UIButton *)sender {
    switch (sender.tag) {
        case 0:
            NSLog(@"旅游新闻");
            self.url = @"news/news_list.json";
            break;
        case 1:
            self.url = @"news/hp_list.json";
            NSLog(@"黄陂概要");
            break;
        case 2:
            self.url = @"news/hotnews_list.json";
            NSLog(@"热点关注");
            break;
        case 3:
            self.url = @"news/festivite_list.json";
            NSLog(@"节庆活动");
            break;
        default:
            break;
    }
    [self.tableView headerBeginRefreshing];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return  self.itemArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    MsgCell *cell = [MsgCell cellWithTableView:tableView];
    [cell initData:self.itemArray[indexPath.row]];
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return MSGCELLHEIGHT;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    MsgDetailCtrl *ctrl =   [[MsgDetailCtrl alloc]initWithData:self.itemArray[indexPath.row]];
    [self.navigationController pushViewController: ctrl animated:YES];
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    [self.view endEditing:YES];
}

@end
